package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.LocationConverter;
import com.youlai.system.mapper.SysLocationMapper;
import com.youlai.system.model.entity.SysLocation;
import com.youlai.system.model.form.LocationForm;
import com.youlai.system.model.query.LocationPageQuery;
import com.youlai.system.model.vo.LocationPageVO;
import com.youlai.system.service.SysLocationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 培训地点业务处理实现
 */
@Service
@RequiredArgsConstructor
public class SysLocationServiceImpl extends ServiceImpl<SysLocationMapper, SysLocation> implements SysLocationService {

    private final LocationConverter locationConverter;
    @Override
    public SysLocation getByContentEqual(String content) {
        LambdaQueryWrapper<SysLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysLocation::getContent, content);
        return getOne(queryWrapper);
    }

    @Override
    public SysLocation saveByContent(String content) {
        SysLocation location = new SysLocation();
        location.setContent(content);
        save(location);
        return location;
    }

    @Override
    public Page<LocationPageVO> getLocationPage(LocationPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(keywords), SysLocation::getContent, keywords);
        //查询数据
        Page<SysLocation> categoryPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        return locationConverter.entity2Page(categoryPage);
    }

    @Override
    public boolean saveLocation(LocationForm locationForm) {
        String content = locationForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysLocation>().eq(SysLocation::getContent, content));
        Assert.isTrue(count == 0, "培训地点已存在");

        // 实体转换
        SysLocation category = locationConverter.form2Entity(locationForm);
        return save(category);
    }

    @Override
    public boolean updateLocation(Long locationId, LocationForm locationForm) {
        String content = locationForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysLocation>()
                .eq(SysLocation::getContent, content)
                .ne(SysLocation::getId, locationId)
        );
        Assert.isTrue(count == 0, "培训地点已存在");

        // form -> entity
        SysLocation entity = locationConverter.form2Entity(locationForm);

        // 修改分类
        return this.updateById(entity);
    }

    @Override
    public LocationForm getLocationForm(Long locationId) {
        SysLocation entity = this.getById(locationId);
        return locationConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteLocations(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "培训地点删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteLocations(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Option> listLocationOptions() {
        // 查询数据
        List<SysLocation> locationList = this.list(new LambdaQueryWrapper<SysLocation>()
                .select(SysLocation::getId, SysLocation::getContent)
                .orderByAsc(SysLocation::getSort)
        );

        // 实体转换
        return locationConverter.entities2Options(locationList);
    }
}
